package com.hjm.admin.moule.viewmodel.activity;

import android.content.Context;

import com.allen.library.RxHttpUtils;
import com.allen.library.interceptor.Transformer;
import com.allen.library.utils.ToastUtils;
import com.hjm.admin.moule.api.BookService;
import com.hjm.admin.moule.api.UserService;
import com.hjm.admin.moule.db.entity.BookChapterBean;
import com.hjm.admin.moule.db.entity.CollBookBean;
import com.hjm.admin.moule.db.helpter.CollBookHelper;
import com.hjm.admin.moule.model.BookBean;
import com.hjm.admin.moule.model.BookChaptersBean;
import com.hjm.admin.moule.model.DeleteBookBean;
import com.hjm.admin.moule.utils.rxhelper.RxObserver;
import com.hjm.admin.moule.view.fragment.IBookDetail;
import com.hjm.admin.moule.viewmodel.BaseViewModel;

import java.util.ArrayList;
import java.util.List;

public class VMBookDetailInfo extends BaseViewModel {
    IBookDetail iBookDetail;

    public VMBookDetailInfo(Context mContext, IBookDetail iBookDetail) {
        super(mContext);
        this.iBookDetail = iBookDetail;
    }

    public void bookInfo(String bookid) {
        iBookDetail.showLoading();
        RxHttpUtils.getSInstance().addHeaders(tokenMap()).createSApi(BookService.class)
                .bookInfo(bookid)
                .compose(Transformer.switchSchedulers())
                .subscribe(new RxObserver<BookBean>() {
                    @Override
                    protected void onError(String errorMsg) {
                        iBookDetail.stopLoading();
                    }

                    @Override
                    protected void onSuccess(BookBean data) {
                        iBookDetail.stopLoading();
                        iBookDetail.getBookInfo(data);
                    }
                });
    }

    public void addBookShelf(CollBookBean collBookBean) {
        iBookDetail.showLoading();
        RxHttpUtils.getSInstance().addHeaders(tokenMap()).createSApi(BookService.class)
                .bookChapters(collBookBean.get_id())
                .compose(Transformer.switchSchedulers())
                .subscribe(new RxObserver<BookChaptersBean>() {
                    @Override
                    protected void onError(String errorMsg) {
                        iBookDetail.stopLoading();
                    }

                    @Override
                    protected void onSuccess(BookChaptersBean data) {
                        iBookDetail.stopLoading();
                        List<BookChapterBean> bookChapterBeans = new ArrayList<>();
                        for (BookChaptersBean.ChatpterBean bean : data.getChapters()) {
                            BookChapterBean chapterBean = new BookChapterBean();
                            chapterBean.setBookId(data.getBook());
                            chapterBean.setLink(bean.getLink());
                            chapterBean.setTitle(bean.getTitle());
                            bookChapterBeans.add(chapterBean);
                        }
                        collBookBean.setBookChapters(bookChapterBeans);
                        CollBookHelper.getsInstance().saveBookWithAsync(collBookBean);
                    }
                });
    }

    public void addBookShelfToServer(String bookid) {
        RxHttpUtils.getSInstance().addHeaders(tokenMap()).createSApi(UserService.class)
                .addBookShelf(bookid)
                .compose(Transformer.switchSchedulers())
                .subscribe(new RxObserver<String>() {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String data) {
                        ToastUtils.showToast(data);
                    }
                });
    }

    public void deleteBookShelfToServer(CollBookBean mCollBookBean) {
        DeleteBookBean bean = new DeleteBookBean();
        bean.setBookid(mCollBookBean.get_id());
        RxHttpUtils.getSInstance().addHeaders(tokenMap()).createSApi(UserService.class)
                .deleteBookShelf(bean)
                .compose(Transformer.switchSchedulers())
                .subscribe(new RxObserver<String>() {
                    @Override
                    protected void onError(String errorMsg) {

                    }
                    @Override
                    protected void onSuccess(String data) {
                        ToastUtils.showToast(data);
                        CollBookHelper.getsInstance().removeBookInRx(mCollBookBean);
                    }
                });
    }
}
